package com.yrline.pay.proxy;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.yrline.pay.proxy.request.CommonRequest;
import com.yrline.pay.proxy.response.CommonResponse;
import com.yrline.pay.proxy.response.SuperResponse;
import com.yrline.pay.util.httpclient.HttpsAction;
import com.yrline.pay.util.httpclient.Request;
import com.yrline.pay.util.httpclient.Response;
import com.yrline.pay.util.httpclient.Site;
import com.yrline.pay.util.ips.DES;
import com.yrline.pay.util.ips.MD5;
import com.yrline.pay.web.controller.AccessController;

@Component
public class PayInvoke {

	private Site s= new Site("UFund.ips.com");
	
	private String md5SYS="Ys6z7H93z9h3kQll7tv02SUsjWDcVsatanaPuE4NMbfGLLDOoaAhN7hN9eUxzx45wGT3Ch8De1XwPvRNF0n7GqrnbWRmnlVbxZEs7n6og5229XUveYq9sENyEP5CEsLr";
	
	private String desEncryptKey="r0uScmDuH5FLO37AJV2FN72J";
	
	private String desiv="1eX24DCe";
	
	public static String MER_ID="1184980025";
	
	private Logger logger = org.slf4j.LoggerFactory
			.getLogger(PayInvoke.class);
	
	public <T extends SuperResponse> SuperResponse invoke(CommonRequest request,Class<T> clazz){
		sign(request);
		Request _r = new Request();
		_r.setTimeout(30000);
		_r.setUrl("http://180.168.26.114:20010/p2p-deposit/gateway.htm");
		_r.addParam("operationType", request.getOperationType());
		_r.addParam("merchantID", request.getMerchantID());
		_r.addParam("request", request.getRequestBody());
		_r.addParam("sign", request.getSign());
		Response response =HttpsAction.getInstance().doRequest(_r, s);
		if(response !=null && StringUtils.isNoneBlank(response.getRawText())){
			logger.info(response.getRawText());
			CommonResponse responseObj = JSON.parseObject(response.getRawText(), CommonResponse.class);
			return unSign(responseObj, clazz);
		}
		return null;
	}
	
	public void sign(CommonRequest request){
		request.setMerchantID(String.valueOf(1184980025));
		String requestParam =request.getRequestObj()==null?"":JSON.toJSONString(request.getRequestObj());
		logger.info(requestParam);
		String des = DES.encrypt3DES(requestParam, desEncryptKey, desiv);
		request.setRequestBody(des);
		request.setSign(md5Param(request));
		
		
	}
	public  <T extends SuperResponse> SuperResponse unSign(CommonResponse response,Class<T> clazz){
		 boolean isOk=MD5.verify(response.getMerchantID() + response.getResultCode() + response.getResultMsg()+response.getResponse()+md5SYS,response.getSign(),"UTF-8");
		 
		 if(isOk && response.getResultCode().matches("[0]+")){
			 String responseResult = response.getResponse();
			 String responsePlantText=DES.decrypt3DES(responseResult,desEncryptKey,desiv);
			 SuperResponse returnObj= JSON.parseObject(responsePlantText, clazz);
			 returnObj.setIpsReturnCode(response.getResultCode());
			 returnObj.setIpsReturnMsg(response.getResultMsg());
			 return returnObj;
		 }else{
			 SuperResponse returnObj= new SuperResponse();
			 returnObj.setIpsReturnCode(response.getResultCode());
			 returnObj.setIpsReturnMsg(response.getResultMsg());
			 return returnObj;
		 }
	}
	
	private String md5Param(CommonRequest request){
		
		return MD5.sign(request.getOperationType()+MER_ID+request.getRequestBody()+md5SYS,"UTF-8");
		
	}
}
